<?php


namespace App\Http\Controllers\admin\service;


use App\Http\Controllers\admin\model\BrandModel;
use App\Http\Controllers\admin\model\ClassifyModel;
use App\Http\Controllers\admin\model\GradeModel;
use App\Http\Controllers\admin\model\TopIcModel;
use App\Http\Controllers\utils\LogUtil;
use App\Http\Controllers\utils\ToolUtil;
use App\Http\Controllers\utils\UploadUtil;
use Illuminate\Support\Facades\DB;

class MaintainService
{
    /**
     * 增加应用分类信息
     * @param $param
     * @return false|string
     */
    public static function addClassify($param){
        $param['remark'] = '增加分类信息';
        if(DB::table('t_subject')->where(['grade_id'=>$param['gId'],'name'=>$param['name']])->value('id')){
            return ToolUtil::returnJson(301,'存在相同类型值');
        }
        $int = DB::table('t_subject')->insert(['name'=>$param['name'],'grade_id'=>$param['gId'],'img_url'=>$param['imgUrl'],
            'create_time'=>time()]);
        if($int){
            $param['details'] = ToolUtil::handleSuccessOrError(['imgUrl'=>$param['imgUrl'],'grade_id'=>$param['gId']]);
            LogUtil::addAdminLog($param);
            return ToolUtil::returnJson(200,'成功');
        }
        return ToolUtil::returnJson(300,'失败');
    }

    /**
     * 获取应用分类信息
     * @param $param
     * @return false|string
     */
    public static function queryClassify($param){
        $where = [];
        if(isset($param['name']) && !empty($param['name'])){
            $where[0] = " s.name='$param[name]' ";
        }
        $str = '';
        if($where){
            $str = ' where '.implode('and',$where);
        }
        $count = DB::select("select count('*') as id from t_subject as s left join t_grade as g on s.grade_id=g.id $str");
        $field = "s.id,s.name,g.title,s.img_url,s.status,s.create_time";
        $limit = ToolUtil::calculationPageTwo($param,$count[0]['id']);
        $data = DB::select("select $field from t_subject as s left join t_grade as g on s.grade_id=g.id $str $limit");
        if(count($data)){
            return ToolUtil::returnJson(200,'成功',$data,$count[0]['id']);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 处理获取全部的数据
     * @param $data
     * @return mixed
     */
    public static function checkDataAll($data){
        foreach ($data['data'] as $key=>$value){
            $gradeIdArr[$key] = explode(',',$value['grade_id']);
            $gradeArr = [];
            foreach ($gradeIdArr[$key] as $k=>$v){
                $gradeArr[$k] = GradeModel::queryValue(['id'=>$v],'title');
            }
            $gradeName = implode(',',$gradeArr);
            $value['grade_id'] = $gradeName;
            $data['data'][$key] = $value;
        }
        return $data;
    }

    /**
     * 删除应用分类
     * @param $param
     * @return false|string
     */
    public static function deleteClassify($param){
        $param['remark'] = '删除应用分类';
        $classify = ClassifyModel::queryFirst(['id'=>$param['id']],['name','img_url']);
        if($classify){
            $param['details'] = ToolUtil::handleSuccessOrError(['name'=>$classify['name'],'imgUrl'=>$classify['img_url']]);
            LogUtil::addAdminLog($param);
        }
        $int = DB::table('t_subject')->where('id',$param['id'])->delete();
        if($int){
            return ToolUtil::returnJson(200,'成功');
        }
        return ToolUtil::returnJson(300,'失败');
    }

    /**
     * 编号获取应用分类信息
     * @param $param
     * @return false|string
     */
    public static function queryIdClassify($param){
        $field = ['id','name','img_url','grade_id'];
        $data = DB::table('t_subject')->where('id',$param['id'])->first($field);
        if($data){
            return ToolUtil::returnJson(200,'成功',$data);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 处理编号获取的数据
     * @param $data
     * @return mixed
     */
    public static function checkDataId($data){
        $gradeIdArr = explode(',',$data['grade_id']);
        $gradeArr = [];
        foreach ($gradeIdArr as $k=>$v){
            $gradeArr[$k] = GradeModel::queryValue(['id'=>$v],'title');
        }
        $gradeName = implode(',',$gradeArr);
        $data['grade_id'] = $gradeName;
        return $data;
    }

    /**
     * 编辑应用分类信息
     * @param $param
     * @return false|string
     */
    public static function editClassify($param){
        $param['remark'] = '编辑应用分类';
        $classify = ClassifyModel::queryFirst(['name'=>$param['name']],['id']);
        if($classify){
            if($classify['id']!=$param['id']){
                return ToolUtil::returnJson(301,'存在相同名称');
            }
        }
        $int = DB::table('t_subject')->where('id',$param['id'])->update(['name'=>$param['name'],'img_url'=>$param['imgUrl'],
            'grade_id'=>$param['gId'],'update_time'=>time()]);
        if($int){
            $param['details'] = ToolUtil::handleSuccessOrError(['name'=>$param['name'],'imgUrl'=>$param['imgUrl']]);
            return ToolUtil::returnJson(200,'成功');
        }
        return ToolUtil::returnJson(300,'失败');
    }

    /**
     * 增加应用品牌信息
     * @param $param
     * @return false|string
     */
    public static function addBrand($param){
        $param['remark'] = '增加应用品牌';
        $gNameArr = explode(',',$param['gName']);
        $gradeArr = [];
        foreach ($gNameArr as $k=>$v){
            $gradeArr[$k] = GradeModel::queryValue(['title'=>$v],'id');
        }
        $gradeName = '';
        foreach ($gradeArr as $k=>$v){
            $classify = BrandModel::queryLikeFirst(['name'=>$param['name']],['field'=>'grade_id','grade_id'=>$v],['id']);
            if($classify){
                return ToolUtil::returnJson(301,'存在相同班级信息');
            }else{
                $gradeName = implode(',',$gradeArr);
                continue;
            }
        }
        $int = BrandModel::add(['name'=>$param['name'],'img_url'=>$param['imgUrl'],'grade_id'=>$gradeName,'create_time'=>time()]);
        if($int){
            $param['details'] = ToolUtil::handleSuccessOrError(['name'=>$param['name'],'imgUrl'=>$param['imgUrl']]);
            return ToolUtil::returnJson(200,'成功');
        }
        return ToolUtil::returnJson(300,'失败');
    }

    /**
     * 获取应用品牌信息
     * @param $param
     * @return false|string
     */
    public static function queryBrand($param){
        switch ($param['type']){
            case 1:
                $data = self::handleQueryBrand([],$param);
                break;
            case 2:
                $param['field'] = 'name';
                $data = self::handleQueryBrand([],$param);
                break;
            default:
                $data['data'] = null;
        }
        if(count($data['data'])){
            $result = self::checkDataAll($data);
            return ToolUtil::returnJson(200,'成功',$result['data'],$result['count']);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 处理获取应用品牌信息
     * @param array $where
     * @param $param
     * @return mixed
     */
    public static function handleQueryBrand($where=[],$param){
        if($where){
            $data['count'] = BrandModel::queryCount($where,$param);
            $page = ToolUtil::calculationPage($param,$data['count']);
            $data['data'] = BrandModel::queryAll($where,$param,$page);
        }else{
            $data['count'] = BrandModel::queryCount([],$param);
            $page = ToolUtil::calculationPage($param,$data['count']);
            $data['data'] = BrandModel::queryAll([],$param,$page);
        }
        return $data;
    }

    /**
     * 删除应用品牌信息
     * @param $param
     * @return false|string
     */
    public static function deleteBrand($param){
        $param['remark'] = '删除应用品牌';
        $brand = BrandModel::queryFirst(['id'=>$param['id']],['name','img_url']);
        if($brand){
            $param['details'] = ToolUtil::handleSuccessOrError(['name'=>$brand['name'],'imgUrl'=>$brand['img_url']]);
            LogUtil::addAdminLog($param);
        }
        $int = BrandModel::delete(['id'=>$param['id']]);
        if($int){
            return ToolUtil::returnJson(200,'成功');
        }
        return ToolUtil::returnJson(300,'失败');
    }

    /**
     * 编号获取应用品牌信息
     * @param $param
     * @return false|string
     */
    public static function queryIdBrand($param){
        $field = ['id','name','img_url','grade_id'];
        $data = BrandModel::queryFirst(['id'=>$param['id']],$field);
        if($data){
            $result = self::checkDataId($data);
            return ToolUtil::returnJson(200,'成功',$result);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 编辑应用品牌信息
     * @param $param
     * @return false|string
     */
    public static function editBrand($param){
        $param['remark'] = '编辑应用品牌';
        $Brand= BrandModel::queryFirst(['name'=>$param['name']],['id']);
        if($Brand){
            if($Brand['id']!=$param['id']){
                return ToolUtil::returnJson(301,'存在相同名称');
            }
        }
        $gradeNameArr = explode(',',$param['gName']);
        $gradeArr = [];
        foreach ($gradeNameArr as $k=>$v){
            $gradeArr[$k] = GradeModel::queryValue(['title'=>$v],'id');
        }
        $gradeName = implode(',',$gradeArr);
        $int = BrandModel::edit(['id'=>$param['id']],['name'=>$param['name'],'img_url'=>$param['imgUrl'],'grade_id'=>$gradeName,'update_time'=>time()]);
        if($int){
            $param['details'] = ToolUtil::handleSuccessOrError(['name'=>$param['name'],'imgUrl'=>$param['imgUrl']]);
            return ToolUtil::returnJson(200,'成功');
        }
        return ToolUtil::returnJson(300,'失败');
    }

    /**
     * 增加应用专题信息
     * @param $param
     * @return false|string
     */
    public static function addTopic($param){
        $param['remark'] = '增加应用专题';
        $gNameArr = explode(',',$param['gName']);
        $gradeArr = [];
        foreach ($gNameArr as $k=>$v){
            $gradeArr[$k] = GradeModel::queryValue(['title'=>$v],'id');
        }
        $gradeName = '';
        foreach ($gradeArr as $k=>$v){
            $classify = TopIcModel::queryLikeFirst(['name'=>$param['name']],['field'=>'grade_id','grade_id'=>$v],['id']);
            if($classify){
                return ToolUtil::returnJson(301,'存在相同班级信息');
            }else{
                $gradeName = implode(',',$gradeArr);
                continue;
            }
        }
        $int = TopIcModel::add(['name'=>$param['name'],'img_url'=>$param['imgUrl'],'grade_id'=>$gradeName,'create_time'=>time()]);
        if($int){
            $param['details'] = ToolUtil::handleSuccessOrError(['name'=>$param['name'],'imgUrl'=>$param['imgUrl']]);
            return ToolUtil::returnJson(200,'成功');
        }
        return ToolUtil::returnJson(300,'失败');
    }

    /**
     * 获取应用专题信息
     * @param $param
     * @return false|string
     */
    public static function queryTopic($param){
        switch ($param['type']){
            case 1:
                $data = self::handleQueryTopic([],$param);
                break;
            case 2:
                $param['field'] = 'name';
                $data = self::handleQueryTopic([],$param);
                break;
            default:
                $data['data'] = null;
        }
        if(count($data['data'])){
            $result = self::checkDataAll($data);
            return ToolUtil::returnJson(200,'成功',$result['data'],$result['count']);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 处理获取应用专题信息
     * @param array $where
     * @param $param
     * @return mixed
     */
    public static function handleQueryTopic($where=[],$param){
        if($where){
            $data['count'] = TopIcModel::queryCount($where,$param);
            $page = ToolUtil::calculationPage($param,$data['count']);
            $data['data'] = TopIcModel::queryAll($where,$param,$page);
        }else{
            $data['count'] = TopIcModel::queryCount([],$param);
            $page = ToolUtil::calculationPage($param,$data['count']);
            $data['data'] = TopIcModel::queryAll([],$param,$page);
        }
        return $data;
    }

    /**
     * 编号获取应用专题信息
     * @param $param
     * @return false|string
     */
    public static function queryIdTopic($param){
        $field = ['id','name','img_url','grade_id'];
        $data = TopIcModel::queryFirst(['id'=>$param['id']],$field);
        if($data){
            $result = self::checkDataId($data);
            return ToolUtil::returnJson(200,'成功',$result);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 编辑应用专题信息
     * @param $param
     * @return false|string
     */
    public static function editTopic($param){
        $param['remark'] = '编辑应用专题';
        $topic= TopIcModel::queryFirst(['name'=>$param['name']],['id']);
        if($topic){
            if($topic['id']!=$param['id']){
                return ToolUtil::returnJson(301,'存在相同名称');
            }
        }
        $gradeNameArr = explode(',',$param['gName']);
        $gradeArr = [];
        foreach ($gradeNameArr as $k=>$v){
            $gradeArr[$k] = GradeModel::queryValue(['title'=>$v],'id');
        }
        $gradeName = implode(',',$gradeArr);
        $int = TopIcModel::edit(['id'=>$param['id']],['name'=>$param['name'],'img_url'=>$param['imgUrl'],'grade_id'=>$gradeName,'update_time'=>time()]);
        if($int){
            $param['details'] = ToolUtil::handleSuccessOrError(['name'=>$param['name'],'imgUrl'=>$param['imgUrl']]);
            return ToolUtil::returnJson(200,'成功');
        }
        return ToolUtil::returnJson(300,'失败');
    }

    /**
     * 删除应用专题
     * @param $param
     * @return false|string
     */
    public static function deleteTopic($param){
        $param['remark'] = '删除应用专题';
        $brand = TopIcModel::queryFirst(['id'=>$param['id']],['name','img_url']);
        if($brand){
            $param['details'] = ToolUtil::handleSuccessOrError(['name'=>$brand['name'],'imgUrl'=>$brand['img_url']]);
            LogUtil::addAdminLog($param);
        }
        $int = TopIcModel::delete(['id'=>$param['id']]);
        if($int){
            return ToolUtil::returnJson(200,'成功');
        }
        return ToolUtil::returnJson(300,'失败');
    }

    /**
     * 上传图片
     * @param $param
     * @return false|string
     */
    public static function uploadMaintainImg($param){
        $param['size'] = 1*1024*1024;
        $param['width'] = 800;
        $param['height'] = 800;
        return UploadUtil::uploadImg($param,$param['file']);
    }
}